package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.manor.FishData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ：zfr
 * @date ：2021/10/14 11:22
 */
public class UserFishPondFishDataDaoImpl extends TempDataObjectDao<FishData> {
    private static String insertSql = "insert into t_u_fishpond_fish(userId,fishId,num,highestWeight,equipPatronsId," +
            "lv,isReadSample,isShow,isGetUnLockReward,equipTimes,equipBuyTimes,lastUpdateTime,accFishingCount,realFishingCount) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(FishData data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getFishId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getNum()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getHighestWeight()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEquipPatronsId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLv()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isReadSample()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isShow()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isGetUnLockReward()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEquipTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEquipBuyTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastUpdateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getAccFishingCount()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getRealFishingCount()));
        return getDb().executeNoneQuery(insertSql, para) > 0;
    }

    private static String updateSql = "update t_u_fishpond_fish set num=?,highestWeight=?,equipPatronsId=?,lv=?," +
            "isReadSample=?,isShow=?,isGetUnLockReward=?,equipTimes=?,equipBuyTimes=?,lastUpdateTime =?,accFishingCount=?,realFishingCount=? where userId=? and fishId=?";
    @Override
    protected boolean updateData(FishData data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getNum()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getHighestWeight()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEquipPatronsId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLv()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isReadSample()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isShow()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isGetUnLockReward()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEquipTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEquipBuyTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastUpdateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getAccFishingCount()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getRealFishingCount()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getFishId()));
        return getDb().executeNoneQuery(updateSql, para) > 0;
    }

    @Override
    protected FishData parseData(ResultSet rs) throws SQLException {
        FishData data = new FishData();
        data.setUserId(rs.getLong("userId"));
        data.setFishId(rs.getInt("fishId"));
        data.setNum(rs.getLong("num"));
        data.setHighestWeight(rs.getLong("highestWeight"));
        data.setEquipPatronsId(rs.getInt("equipPatronsId"));
        data.setLv(rs.getInt("lv"));
        data.setReadSample(rs.getBoolean("isReadSample"));
        data.setGetUnLockReward(rs.getBoolean("isGetUnLockReward"));
        data.setShow(rs.getBoolean("isShow"));
        data.setEquipTimes(rs.getInt("equipTimes"));
        data.setEquipBuyTimes(rs.getInt("equipBuyTimes"));
        data.setLastUpdateTime(rs.getInt("lastUpdateTime"));
        data.setAccFishingCount(rs.getLong("accFishingCount"));
        data.setRealFishingCount(rs.getLong("realFishingCount"));
        return data;
    }

    public static String getSql = "select * from t_u_fishpond_fish where userId=?";

    public Map<Integer, FishData> getFishDataMap(long playerId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, FishData> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getSql);
            call.setLong(1, playerId);
            rs = call.executeQuery();
            while (rs.next()) {
                FishData data = parse(rs);
                map.put(data.getFishId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }


    public Map<Long,Map<Integer, FishData>> getSomeFishDataMap(List<Long> userIdList) {
        Map<Long,Map<Integer, FishData>> map = new ConcurrentHashMap<>();
        if(userIdList.size() == 0){
            return map;
        }
        String getSql = "select * from t_u_fishpond_fish where userId in("+ StringUtils.listToString(userIdList,",") +")";
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getSql);
            rs = call.executeQuery();
            while (rs.next()) {
                FishData data = parse(rs);
                if(!map.containsKey(data.getUserId())){
                    map.put(data.getUserId(),new ConcurrentHashMap<>());
                }
                map.get(data.getUserId()).put(data.getFishId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }
}
